package com.hj.recyclerview;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;


public abstract class BaseAdapter<T extends Object> extends RecyclerView.Adapter<BaseViewHolder>
        implements ItemSlideHelper.Callback {

    protected List<T> mData;
    private static final int TYPE_ITEM = 0;
    private static final int TYPE_FOOTER = 1;
    private static final int TYPE_HEADER = 2;
    public RecyclerView recyclerView;
    private boolean hasHeaderView, hasLoadmoreView;
    private View headerView;
    private OnItemClickListener l;
    protected int curSelectIndex = 0;

    public interface OnItemClickListener<T> {
        void onClick(int position, T t);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        l = listener;
    }

    private boolean isListEmpty(List<T> d) {
        if (d != null || d.size() > 0) {
            return false;
        }
        return true;
    }

    public BaseAdapter(List<T> d) {
        if (isListEmpty(d)) {
            mData = new ArrayList<T>();
        } else {
            mData = d;
        }
        hasHeaderView = hasHeaderView();
        hasLoadmoreView = hasLoadmoreView();
        curSelectIndex = 0;
    }

    public void setCurSelectIndex(int index) {
        this.curSelectIndex = index;
        notifyDataSetChanged();
    }

    public int getCurSelectIndex() {
        return curSelectIndex;
    }

    public void reSetData(List<T> d) {
        mData.clear();
        curSelectIndex = 0;
        if (!isListEmpty(d)) {
            mData.addAll(d);
            notifyDataSetChanged();
        }
    }

    public void addData(List<T> d) {
        mData.addAll(d);
        curSelectIndex = 0;
        notifyDataSetChanged();
    }

    /**
     * 删除单条数据
     *
     * @param position
     */
    public void removeData(int position) {
        if (curSelectIndex > position) {
            curSelectIndex--;
        } else if (curSelectIndex == position) {
            curSelectIndex = 0;
        }
        mData.remove(position);
        notifyDataSetChanged();
    }

    public void addData(T d) {
        mData.add(d);
        notifyDataSetChanged();
    }

    public void setData(int position, T d) {
        mData.set(position, d);
        notifyDataSetChanged();
    }

    protected abstract boolean hasHeaderView();

    protected abstract boolean hasLoadmoreView();

    private boolean hasMoreData = true;

    public void setHasMoreData(boolean b) {
        hasMoreData = b;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0 && headerView != null && hasHeaderView) {
            return TYPE_HEADER;
        } else if (position + 1 == getItemCount() && hasLoadmoreView) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }


    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        BaseViewHolder holder = null;
        switch (viewType) {
            case TYPE_HEADER:
                holder = new BaseViewHolder(headerView);
                break;
            case TYPE_FOOTER:
//                View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.pty_loadmore_item, parent, false);
//                ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 40);
//                v.setLayoutParams(layoutParams);
//                holder = new BaseViewHolder(v);
                break;
            case TYPE_ITEM:
                int resId = getItemLayoutId();
                View itemView = LayoutInflater.from(parent.getContext()).inflate(resId, parent, false);
                holder = new BaseViewHolder(itemView);
                break;
        }
        return holder;
    }


    @Override
    public void onBindViewHolder(BaseViewHolder holder, final int position) {
        if (position == getItemCount() - 1 && hasLoadmoreView) {
            if (!hasMoreData) {

            }
        } else if (headerView != null && position == 0) {
            mBindHeaderViewHolder(holder);
        } else {
            mBindViewHolder(holder, position - getHeaderViewCount());
            if (l != null) {
                holder.getItemView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        curSelectIndex = position - getHeaderViewCount();
                        notifyDataSetChanged();
                        l.onClick(position - getHeaderViewCount(), mData.get(position));
                    }
                });
            }
        }
    }

    public void mBindHeaderViewHolder(BaseViewHolder holder) {

    }

    public abstract void mBindViewHolder(BaseViewHolder holder, int position);

    public abstract int getItemLayoutId();

    @Override
    public int getItemCount() {
        int size;
        if (mData != null && mData.size() != 0) {
            size = mData.size();
            if (hasHeaderView)
                size = size + getHeaderViewCount();
            if (hasLoadmoreView)
                size = size + 1;
            return size;
        }
        return 0;
    }


    public int getHeaderViewCount() {
        return headerView == null ? 0 : 1;
    }

    public void addHeader(View view) {
        headerView = view;
        notifyDataSetChanged();
    }

    public View getHeaderView() {
        return headerView;
    }


    protected boolean canSwipe() {
        return false;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        if (canSwipe()) {
            this.recyclerView = recyclerView;
            this.recyclerView.addOnItemTouchListener(new ItemSlideHelper(recyclerView.getContext(), this));
        }
    }

    @Override
    public boolean isToSwipe(RecyclerView.ViewHolder holder, int position) {
        return false;
    }

    @Override
    public int getHorizontalRange(RecyclerView.ViewHolder holder) {
        return 0;
    }

    @Override
    public int getChildAdapterPosition(View childView) {
        return recyclerView.getChildAdapterPosition(childView);
    }

    @Override
    public RecyclerView.ViewHolder getChildViewHolder(View childView) {
        return recyclerView.getChildViewHolder(childView);
    }

    @Override
    public View findTargetView(float x, float y) {
        return recyclerView.findChildViewUnder(x, y);
    }

    @Override
    public boolean isClickToCollapse(View view, RecyclerView.ViewHolder holder) {
        return true;
    }
}
